<?php 
namespace frontend\controllers;
use yii\web\Controller;
use frontend\models\company;
use frontend\models\job;
use yii;
class CompanyController extends Controller{
	public $layout = 'my';

	public function actionIndex(){
		return $this->render('index');
	}

	public function actionLogin(){
		$request = yii::$app->request;
		if($request->isGET){
			return $this->render('index');
		}else{
			$data = $request->post('Company');

			$sql = "select * from recruit_company where CEmail='".$data['CEmail']."' and CPassword='".$data['CPassword']."'";
			
			if($res =Company::findBySql($sql)->asArray()->one()){
				   $cookies = Yii::$app->response->cookies;
					$cookies->add(new \yii\web\Cookie([
					    'name' => 'company',
					    'value' => serialize(array('CTelName'=>$res['CTelName'],'CId'=>$res['CId'])),
					]));
				
				return $this->redirect('?r=company/index');
			}else{
				 
				return $this->redirect('?r=company/index');
			}

		}
	}

	public function actionRegist(){

		$request = yii::$app->request;
		if($request->isGET){
				return $this->render('index');
		}else{
			$model = new Company;
			$data = $request->post();
			if($model->load($data)&&$model->validate()){
				$res = $model->save();
				$id = $model->attributes['CId'];
				if($res){
				    $cookies = Yii::$app->response->cookies;
					$cookies->add(new \yii\web\Cookie([
					    'name' => 'company',
					    'value' => serialize(array('CTelName'=>$data['Company']['CTelName'],'CId'=>$id)),
					]));
					return $this->redirect('?r=company/index');
				} 
			}else{
		
				return $this->redirect('?r=company/index');
			}
		}
	}
	public function actionCompany_reg(){
		$request = yii::$app->request;
		$model = new Company;
		if($request->isGET){
			$cookies = Yii::$app->request->cookies;
		    $user = unserialize($cookies->getValue('company'));
		    $id = $user['CId'];
		    $res = $model->find()->where("CId=".$id)->one();
			if(!empty($res)){
				$model = $res;
			}
		 
			 return $this->render('company_reg',['model'=>$model]);
		}
		if($request->isPOST){
			$data = $request->post('Company');
			$cookies = Yii::$app->request->cookies;
		    $user = unserialize($cookies->getValue('company'));
		    $res = Company::updateAll($data,['CId'=>$user['CId']]);

		     return $this->redirect('?r=company/company_reg');
		}
	   
   }

   //职位列表
    public function actionWork_list(){
    	$model =  new Job;
    	$data = $model->find()->all();

    	return $this->render("work_list",['data'=>$data]);

    }
    
    //职位添加
    public function actionWork_add(){
    	$request = yii::$app->request;
    	$model = new Job; 
    	if($request->isGET){
    		
    		return $this->render('work_add',['model'=>$model]);
    	}

    	if($request->isPOST){
    		$data = $request->post();
    		if($model ->load($data)&&$model->validate()){
    			$res = $model->save();
    			return $this->redirect('?r=company/index');
    		}else{
    			return $this->redirect('?r=company/work_add');
    		}
    	}
    	
    }
    public function actionWork_del(){
    	$id = yii::$app->request->get('JId');
    	$obj = Job::findOne($id);
    	$res= $obj->delete();
    	if($res){
    		return $this->redirect('?r=company/work_list');
    	}
    }

    public function actionWork_fabu(){

    	$id = yii::$app->request->get('JId');
    	$customer = Job::findOne($id);
    	$customer->JStatus=1;
    	$customer->save();
		return $this->redirect('?r=company/work_list');
    }

    public function actionWork_quxiao(){
    	$id = yii::$app->request->get('JId');
    	
    	$customer = Job::findOne($id);
    	$customer->JStatus=0;
    	$customer->save();
		return $this->redirect('?r=company/work_list');
    }



    //关于我们
	public function actionAbout(){
		return $this->render('about');
	}
	//联系我们
	public function actionContact(){
		return $this->render('contact');
	}
	//退出登录
	public function actionLoginout(){
		$cookies = Yii::$app->response->cookies;
		$cookies->remove('company');
		return $this->redirect('?r=company/index');
	}


}
 ?>